package com.ruoyi.employment.service.impl;

import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.employment.domain.TAssist;
import com.ruoyi.employment.domain.TComment;
import com.ruoyi.employment.mapper.TCommentMapper;
import com.ruoyi.employment.service.ITAssistService;
import com.ruoyi.employment.service.ITCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 学生评价Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-24
 */
@Service
public class TCommentServiceImpl implements ITCommentService
{
    @Autowired
    private TCommentMapper tCommentMapper;

    @Autowired
    private ITAssistService itAssistService;
    /**
     * 查询学生评价
     * 
     * @param id 学生评价主键
     * @return 学生评价
     */
    @Override
    public TComment selectTCommentById(Long id)
    {
        return tCommentMapper.selectTCommentById(id);
    }

    /**
     * 查询学生评价列表
     * 
     * @param tComment 学生评价
     * @return 学生评价
     */
    @Override
    public List<TComment> selectTCommentList(TComment tComment)
    {
        String username = SecurityUtils.getUsername();
        if (username.equals("admin")) {
            username = null;
        }
        tComment.setTeacherName(username);
        return tCommentMapper.selectTCommentList(tComment);
    }

    /**
     * 新增学生评价
     * 
     * @param tComment 学生评价
     * @return 结果
     */
    @Override
    public int insertTComment(TComment tComment)
    {
        TAssist tAssist = itAssistService.selectTAssistById(tComment.getAssisId());
        if (tAssist.getState() == 2) {
            throw new ServiceException("当前帮扶任务已评价，请勿重复提交");
        } else {
            tAssist.setState(2L);
            itAssistService.updateTAssist(tAssist);
        }
        return tCommentMapper.insertTComment(tComment);
    }

    /**
     * 修改学生评价
     * 
     * @param tComment 学生评价
     * @return 结果
     */
    @Override
    public int updateTComment(TComment tComment)
    {
        return tCommentMapper.updateTComment(tComment);
    }

    /**
     * 批量删除学生评价
     * 
     * @param ids 需要删除的学生评价主键
     * @return 结果
     */
    @Override
    public int deleteTCommentByIds(Long[] ids)
    {
        return tCommentMapper.deleteTCommentByIds(ids);
    }

    /**
     * 删除学生评价信息
     * 
     * @param id 学生评价主键
     * @return 结果
     */
    @Override
    public int deleteTCommentById(Long id)
    {
        return tCommentMapper.deleteTCommentById(id);
    }
}
